#include "lyn_apu_uapi.h"
#include "yyjson.h"

#include <assert.h>
#include <stdint.h>
#include <stdio.h>

void json_parse() {
  // Read JSON file, allowing comments and trailing commas
  yyjson_read_flag flags =
      YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
  yyjson_read_err err;
  yyjson_doc *doc = yyjson_read_file("test.json", flags, NULL, &err);

  // Iterate over the root object
  if (doc) {
    yyjson_val *obj = yyjson_doc_get_root(doc);
    yyjson_obj_iter iter;
    yyjson_obj_iter_init(obj, &iter);
    yyjson_val *key, *val;
    while ((key = yyjson_obj_iter_next(&iter))) {
      val = yyjson_obj_iter_get_val(key);
      printf("%s: %s\n", yyjson_get_str(key), yyjson_get_type_desc(val));
    }
  } else {
    printf("read error (%u): %s at position: %ld\n", err.code, err.msg,
           err.pos);
  }

  // Free the doc
  yyjson_doc_free(doc);
}

yyjson_doc *json_open(const char *name)
{
  yyjson_read_flag flags =
      YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
  yyjson_read_err err;
  yyjson_doc *doc = yyjson_read_file("test.json", flags, NULL, &err);

  if (doc == NULL) {
    printf("json read error (%u): %s at position: %ld\n", err.code, err.msg,
           err.pos);
    abort();
  }

  return doc;
}

void json_get(char *node[], char *type) {
}

void print_info(struct apu_info *info) {
  printf("driver version: %s\n", info->ver_drv);
  printf("    so version: %s\n", info->ver_so);
  printf("        online: %d\n", info->online);
  printf("         cores: %d\n", info->cores);
  printf("    used cores: %d\n", info->used_cores);
  printf("        status: %d\n", info->status);
  printf("         clock: %d\n", info->clock);
}

int main(int argc, char *argv[]) {
  json_parse();
  return 0;

  apu_handle handle = lyn_apu_open(0);
  assert(handle);

  int ret = lyn_apu_set_mode(handle, WORK_MODE_CNN);
  assert(ret == 0);

  struct apu_info info = {0};
  ret = lyn_apu_get_info(handle, &info);
  assert(ret == 0);
  assert(info.online != 0);
  print_info(&info);

  struct apu_model model = {0};
  // ret = lyn_apu_set_model(handle, &model);

  printf("hello world!\n");
  return 0;
}
